package zhuyun.code.launcheractivity.view.adapter

import android.view.View
import android.widget.ImageView
import com.google.gson.Gson
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.model.data.CircleListBean
import zhuyun.code.launcheractivity.model.data.CirlceStarDynamicsBean
import zhuyun.code.launcheractivity.model.data.Level0Item
import zhuyun.code.launcheractivity.model.data.interfaces.IExpandable
import zhuyun.code.launcheractivity.utils.GlideUtil
import zhuyun.code.launcheractivity.view.interfaces.MultiItemEntity

/**
 * @author: Administrator
 * @date: 2021/12/4
 */
class HomepageFriendAdapter(val data:MutableList<MultiItemEntity>,var isAllExpand:Boolean, val ivExpand:ImageView):BaseMultiItemQuickAdapter<MultiItemEntity,BaseViewHolder>(data) {

    init {
        addItemType(Constants.TYPE_LEVEL_0, R.layout.item_expandable_lv0)
        addItemType(Constants.TYPE_CIRCLE_DETAILS, R.layout.item_expandable_lv1)
    }
    override fun convert(helper: BaseViewHolder, item: MultiItemEntity) {
        when(helper.itemViewType){
            Constants.TYPE_LEVEL_0->{
                val lv0 = item as Level0Item
                helper.setText(R.id.tv_year,lv0.title)
                helper.setText(R.id.tv_date,lv0.subTitle)
                helper.setVisibility(R.id.tv_year,if(lv0.titleVisible) View.VISIBLE else View.GONE)
                helper.itemView.setOnClickListener{
                    val pos = helper.adapterPosition
                    if(isAllExpand){
                        collapse(pos)
                        ivExpand.setImageResource(R.drawable.icon_freeout)
                        isAllExpand = false
                    } else {
                        if(lv0.isExpanded()){
                            collapse(pos)
                        } else {
                            expand(pos)
                        }
                        var breakFlag = false
                        for(item in data){
                            if(isExpandable(item)){
                                val expandable = item as IExpandable<CircleListBean>
                                if(!expandable.isExpanded()){
                                    breakFlag = true
                                    break
                                }

                            }
                        }
                        if(!breakFlag){
                            ivExpand.setImageResource(R.drawable.icon_allup)
                            isAllExpand = true
                        }
                    }
                }
            }
            Constants.TYPE_CIRCLE_DETAILS->{
                val circleListBean = item as CircleListBean

                helper.setText(R.id.tv_content,circleListBean.TxtContent)
                helper.setVisibility(R.id.rl_dynamic,View.GONE)
                if(circleListBean.CircleTypeID == 1000&&circleListBean.CustomType == 7){
                    val starDynamicsBean = Gson().fromJson(circleListBean.Custom,CirlceStarDynamicsBean::class.java)
                    if(starDynamicsBean.Title.isEmpty()){
                        helper.setVisibility(R.id.rl_dynamic,View.GONE)
                    } else {
                        helper.setVisibility(R.id.rl_dynamic,View.VISIBLE)
                        GlideUtil.displayEspImage(circleListBean.FirstImgUrl,helper.getView(R.id.ivDynamic),R.drawable.pic_touxiang_normal)
                        helper.setText(R.id.tvNickName, starDynamicsBean.Author)
                        helper.setText(R.id.tvDynamic, starDynamicsBean.Title)
                    }
                }
            }
        }

    }


}